package com.g5kj.coupon.ssm.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.g5kj.coupon.ssm.common.AppUtils;
import com.g5kj.coupon.ssm.common.MD5Util;
import com.g5kj.coupon.ssm.common.Result;
import com.g5kj.coupon.ssm.dao.AdminDao;
import com.g5kj.coupon.ssm.model.AdminEntity;
import com.g5kj.coupon.ssm.service.AdminService;


@Service("adminService")
public class AdminServiceImpl implements AdminService{
	
	@Autowired
	private AdminDao adminDao;

	@Override
	public AdminEntity login(String name, String password) {
		AdminEntity admin = adminDao.getAdminByName(name);
		System.out.println(admin);
		if(admin!=null){
			String md5Pass = AppUtils.getPassWrod(password);
			System.out.println(md5Pass);
			if(admin.getPassword().equals(md5Pass)){
				return admin;
			}
		}
		
		return null;
	}
	
	
	

	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}




	@Override
	public Result addAdmin(AdminEntity adminEntity) {
		String md5Pass = AppUtils.getPassWrod(adminEntity.getPassword());
		AdminEntity admin = new AdminEntity();
		AdminEntity isAdmin = adminDao.getAdminByName(adminEntity.getName());
		if(adminEntity.getName()==null||adminEntity.getName()==""){
			return Result.fail("用户名不能为空");
		}
		if(isAdmin!=null){
			return Result.fail("用户名不可用");
		}
		admin.setName(adminEntity.getName());
		admin.setPassword(md5Pass);
		admin.setCreat_time(new Date());
		admin.setIs_stop(0);
		int result = adminDao.addAdmin(admin);
		if(result>0){
			return Result.ok("添加成功");
		}else{
			return Result.ok("添加失败");
		}
		
	}




	@Override
	public List<AdminEntity> listAll() {
		List<AdminEntity> listAll = adminDao.listAll();
		return listAll;
	}




	@Override
	public int delete(Integer id) {
		int result = adminDao.delete(id);
		return result;
	}




	@Override
	public Result updateAdmin(AdminEntity adminEntity) {
		int result = adminDao.updateAdmin(adminEntity);
		if(result>0){
			return Result.ok("修改成功");
		}else{
			return Result.fail("修改失败");
		}
		
	}




	@Override
	public AdminEntity getAdminById(Integer id) {
		
		return adminDao.getAdminById(id);
	}




	@Override
	public Result stop(int id) {
		AdminEntity adminEntity = adminDao.getAdminById(id);
		adminEntity.setIs_stop(1);
		int result = adminDao.stop(adminEntity );
		if(result>0){
			return Result.ok("停用成功");
		}else{
			return Result.fail("停用失败");
		}
		
	}




	@Override
	public Result start(int id) {
		AdminEntity adminEntity = adminDao.getAdminById(id);
		adminEntity.setIs_stop(0);
		int result = adminDao.start(adminEntity);
		if(result>0){
			return Result.ok("启用成功");
		}else{
			return Result.fail("启用失败");
		}
		
	}
	
	
	

}
